package team.ttorder.ttorder.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletResponse;

import org.springframework.web.multipart.MultipartFile;

public class ImageUtils {

	public static void save(String fileName, MultipartFile file, String path) {
		// 存放地址target目录下
		// String savePath = path + "\\static\\img\\dc";
		// savePath = savePath.substring(savePath.indexOf("/"), savePath.length());
		// 备份地址
		String backupPath = "F:\\STSProgram\\img";
		File fileDir = new File(backupPath);
		if (!fileDir.exists()) {
			fileDir.mkdirs();
		}
		try {
			// FileOutputStream os = new FileOutputStream(new File(savePath + "\\" +
			// fileName));
			// os.write(file.getBytes());
			// os.flush();

			FileOutputStream os = new FileOutputStream(new File(backupPath + "\\" + fileName));
			os.write(file.getBytes());
			os.flush();
			os.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/*
	 * public static String browseImage(String fileName, String savePath,
	 * MultipartFile file) { savePath = savePath + "\\static\\browseImage"; //
	 * 除去前缀，获取合法的绝对路径 savePath = savePath.substring(savePath.indexOf("/"),
	 * savePath.length()); System.out.println(savePath); // 第一次存储的时候，文件夹不存在，需要创建
	 * File saveFile = new File(savePath); if (!saveFile.exists()) {
	 * saveFile.mkdirs(); } try { FileOutputStream os = new FileOutputStream(new
	 * File(savePath + "\\" + fileName)); os.write(file.getBytes()); os.flush();
	 * os.close(); } catch (Exception e) { e.printStackTrace(); } return fileName; }
	 */

	public static void delete(String savePath, String fileName) {
		// savePath = savePath + "\\static\\img\\dc";
		// 备份地址
		String backupPath = "F:\\STSProgram\\img";
		// 去掉前缀
		// savePath = savePath.substring(savePath.indexOf("/"), savePath.length());
		// File file = new File(savePath);
		// File[] files = file.listFiles();
		// for (File f : files) {
		// if (f.isFile()) {
		// if (f.getName().equals(fileName)) {
		// f.delete();
		// break;
		// }
		// }
		// }

		File file = new File(backupPath);
		File[] files = null;
		files = file.listFiles();
		for (File f : files) {
			if (f.isFile()) {
				if (f.getName().equals(fileName)) {
					f.delete();
					break;
				}
			}
		}
	}

	public static void getImage(HttpServletResponse response, String name) {
		String path = "F:\\STSProgram\\img";
		File file = new File(path);
		if (!file.exists()) {
			file.mkdirs();
		}
		File[] files = file.listFiles();
		for (File f : files) {
			if (f.isFile()) {
				if (f.getName().equals(name)) {
					try {
						response.setContentType("image/jpeg");
						InputStream is = new FileInputStream(file + "\\" + name);
						int size = is.available();
						byte[] buffer = new byte[size];
						is.read(buffer);
						is.close();

						OutputStream os = response.getOutputStream();
						os.write(buffer);
						os.flush();
						os.close();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}

		}
	}
}
